package lumis.portal.metatag;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import lumis.portal.PortalException;
import lumis.portal.UnexpectedException;
import lumis.portal.dao.DaoException;
import lumis.portal.dao.jdbc.ITransactionJdbc;
import lumis.portal.manager.ManagerException;
import lumis.util.ITransaction;
import lumis.util.JdbcUtil;

/**
 * DAO JDBC implementation for Meta-Tags related data
 *
 * @version $Revision: 7370 $ $Date: 2007-06-12 13:50:49 -0300 (Tue, 12 Jun 2007) $
 * @since 4.0.11
 */
public class MetaTagDao implements IMetaTagDao
{

	public void add(MetaTagConfig metaTagConfig, ITransaction transaction) throws PortalException
	{
		try
		{
			String query = null;
			
			if(metaTagConfig.parentId == null)
				throw new ManagerException("STR_META_TAG_PARENT_ID_REQUIRED");
			
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) transaction;
			Connection connection = daoTransactionJdbc.getConnection();

			if (metaTagConfig.getType() == MetaTagConfig.METATAG_TYPE_CHANNEL)
				query = "INSERT INTO lum_MetaTag (metaTagId, content, parentChannelId, position) VALUES (?, ?, ?, ?)";
			else
				query = "INSERT INTO lum_MetaTag (metaTagId, content, parentPageId, position) VALUES (?, ?, ?, ?)";
				
			PreparedStatement statement = connection.prepareStatement(query);
			
			statement.setString(1, metaTagConfig.getId());
			statement.setString(2, metaTagConfig.getContent());
			statement.setString(3, metaTagConfig.getParentId());
			statement.setInt(4, metaTagConfig.getPosition());
			
			statement.executeUpdate();
			statement.close();
		}
		catch (PortalException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw new UnexpectedException(e);
		}
	}

	public void delete(String id, ITransaction transaction) throws PortalException
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) transaction;
			Connection connection = daoTransactionJdbc.getConnection();
	
			PreparedStatement statement = connection.prepareStatement("DELETE FROM lum_MetaTag WHERE metaTagId = ?");
			
			statement.setString(1, id);
			
			statement.executeUpdate();
			statement.close();
		}
		catch(PortalException e)
		{
			throw e;
		}
		catch(Exception e)
		{
			throw new UnexpectedException(e);
		}
	}

	public MetaTagConfig get(String id, ITransaction transaction) throws PortalException
	{
		try
		{
			MetaTagConfig loadedConfig = null;
			
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) transaction;
			Connection connection = daoTransactionJdbc.getConnection();
			
			PreparedStatement statement = connection.prepareStatement("SELECT metaTagId, parentChannelId, parentPageId, content, position FROM lum_MetaTag WHERE metaTagId = ?");
			statement.setString(1, id);
			
			ResultSet resultSet = statement.executeQuery();
			if (resultSet.next())
			{
				int type = MetaTagConfig.METATAG_TYPE_CHANNEL;
				loadedConfig = new MetaTagConfig();
				loadedConfig.setId(resultSet.getString("metaTagId"));
				loadedConfig.setContent(resultSet.getString("content"));
				loadedConfig.setPosition(resultSet.getInt("position"));
				String parentId = resultSet.getString("parentChannelId");
				if(parentId == null)
				{
					type = MetaTagConfig.METATAG_TYPE_PAGE;
					parentId = resultSet.getString("parentPageId");
				}
				loadedConfig.setType(type);
				
				loadedConfig.setParentId(parentId);
			}
			resultSet.close();
			statement.close();

			return loadedConfig;
		}
		catch (DaoException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw new UnexpectedException(e);
		}
	}
	
	public List<String> getIdsByParentId(String parentId, ITransaction transaction) throws PortalException
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) transaction;
			Connection connection = daoTransactionJdbc.getConnection();

			PreparedStatement statement = connection.prepareStatement("select metaTagId from lum_MetaTag where parentChannelId = ? or parentPageId = ? order by position");
			try
			{
				statement.setString(1, parentId);
				statement.setString(2, parentId);

				List<String> returnValues = JdbcUtil.fillListString(statement, "metaTagId");				
				return returnValues;
			}
			finally
			{				
				statement.close();
			}
		}
		catch (PortalException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw new UnexpectedException(e);
		}
	}
	
	public void update(MetaTagConfig metaTagConfig, ITransaction transaction) throws PortalException
	{
		try
		{
			String query = null;
			
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) transaction;
			Connection connection = daoTransactionJdbc.getConnection();
		
			if (metaTagConfig.getType() == MetaTagConfig.METATAG_TYPE_CHANNEL)
				query = "UPDATE lum_MetaTag SET content = ?, parentChannelId = ?, position = ? WHERE metaTagId = ?";
			else
				query = "UPDATE lum_MetaTag SET content = ?, parentPageId = ?, position = ? WHERE metaTagId = ?";

			PreparedStatement statement = connection.prepareStatement(query);
			
			statement.setString(1, metaTagConfig.getContent());
			statement.setString(2, metaTagConfig.getParentId());
			statement.setInt(3, metaTagConfig.getPosition());
			statement.setString(4, metaTagConfig.getId());
			
			statement.executeUpdate();
			statement.close();
		}
		catch (DaoException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw new UnexpectedException(e);
		}
	}

}
